package com.ruoyi.aiConversation.service.impl;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import com.ruoyi.message.domain.Message;
import com.ruoyi.message.mapper.MessageMapper;
import com.ruoyi.websocket.WebSocketConnect;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.aiConversation.mapper.AiConversationMapper;
import com.ruoyi.aiConversation.domain.AiConversation;
import com.ruoyi.aiConversation.service.IAiConversationService;
import org.springframework.web.socket.WebSocketSession;

/**
 * ai对话Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-02-11
 */
@Service
public class AiConversationServiceImpl implements IAiConversationService 
{
    @Autowired
    private AiConversationMapper aiConversationMapper;


    @Autowired
    private MessageMapper MessageMapper;

    /**
     * 查询ai对话
     * 
     * @param aiConversationId ai对话主键
     * @return ai对话
     */
    @Override
    public AiConversation selectAiConversationByAiConversationId(String aiConversationId)
    {
        return aiConversationMapper.selectAiConversationByAiConversationId(aiConversationId);
    }




    @Override
    public List<AiConversation> selectAiConversationByUserId(AiConversation aiConversation) {
        List<AiConversation> aiConversations = aiConversationMapper.selectAiConversationList(aiConversation);
        System.out.println(aiConversations);
            for (AiConversation aiConversation2 : aiConversations){
                Message test = MessageMapper.selectFirstMessageByAiConversationId(aiConversation2.getAiConversationId());
                if (test !=null){
                    aiConversation2.setFirstMessage(test.getMessageContent());
                }else {
                    aiConversation2.setFirstMessage(null);
                }
            }
        return aiConversations;
    }



    /**
     * 查询ai对话列表
     * 
     * @param aiConversation ai对话
     * @return ai对话
     */
    @Override
    public List<AiConversation> selectAiConversationList(AiConversation aiConversation)
    {
        return aiConversationMapper.selectAiConversationList(aiConversation);
    }

    /**
     * 新增ai对话
     * 
     * @param aiConversation ai对话
     * @return 结果
     */
    @Override
    public int insertAiConversation(AiConversation aiConversation)
    {
        Date currentDate = new Date();
        aiConversation.setTitle("未命名");
        aiConversation.setStartTime(currentDate);
        return aiConversationMapper.insertAiConversation(aiConversation);
    }

    /**
     * 修改ai对话
     * 
     * @param aiConversation ai对话
     * @return 结果
     */
    @Override
    public int updateAiConversation(AiConversation aiConversation)
    {
        return aiConversationMapper.updateAiConversation(aiConversation);
    }

    /**
     * 批量删除ai对话
     * 
     * @param aiConversationIds 需要删除的ai对话主键
     * @return 结果
     */
    @Override
    public int deleteAiConversationByAiConversationIds(String[] aiConversationIds)
    {
        return aiConversationMapper.deleteAiConversationByAiConversationIds(aiConversationIds);
    }

    /**
     * 删除ai对话信息
     * 
     * @param aiConversationId ai对话主键
     * @return 结果
     */
    @Override
    public int deleteAiConversationByAiConversationId(String aiConversationId)
    {
        return aiConversationMapper.deleteAiConversationByAiConversationId(aiConversationId);
    }

//    @Override
//    public String selectTitleByAiConversationId(String aiConversationId) {
//        return aiConversationMapper.selectTitleByAiConversation(aiConversationId);
//    }
//
//    @Override
//    public String selectAiConversationId(Long userId, Date currentDate) {
//        return aiConversationMapper.selectAiConversationId(userId, currentDate);
//    }

}
